﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

namespace TenSevenNine.Web.Templates.ProjectHolder
{
    public partial class _default : Acms.AcmsControls.TemplatePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            dsChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsChildren_SelectData);
            dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound);
            

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }

        public override void DataBind()
        {
            base.DataBind();
            var post = AcmsContext.CurrentPost;

            if (post != null)
            {
                //  var pnlItem = pnlItem;

                

            }
        }

        void dsChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
        {
            e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion));
        }
        void dlFeed_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
        {
            var post = e.DataItem as Post;
            if (post != null)
            {
                var lnkTile = e.Item.FindControl("lnkTile") as HyperLink;
                //var lnkDownload = e.Item.FindControl("lnkDownload") as HyperLink;
                if (lnkTile != null)
                {
                    //lnk.Text = post.DisplayName;
                    //lnk.NavigateUrl = post.Url;
                    lnkTile.ToolTip = post.DisplayName;
                    lnkTile.NavigateUrl = post.Url;

                    var pnlThumb= e.Item.FindControl("pnlThumb") as  Panel;
                    
                    
                    //pnlThumb.BackImageUrl; ;

                    var thumbnail = post.ThumbnailId;// ; FindPostControl("HeroImage");// as ImageContentControlDisplay;
                    
                    if (thumbnail != null)
                    {
                        
                        pnlThumb.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(thumbnail.Value, this.Page);

                        //var prop = thumbnail.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                        //if (prop != null && prop.HasResource())
                        //{
                        //    pnlThumb.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                        //}
                    }

                    //lnk.Attributes.Add("rel", "Next");

                }
            }
        }
        
    }
}